<template>
  <div class="oe-ruleEdit" v-loading="loading">
    <topTip :title="[{ text: '规则管理' }, { text: '查看', color: '#1890ff' }]"></topTip>
    <div class="oe-ruleEdit-content">
      <div class="oe-ruleEdit-set">
        <div class="set-ruleEdit-container">
          <span class="set-ruleEdit-label">规则名称：</span>
          <span class="oe-manage-input">
            <a-input placeholder="请输入考试规则" :disabled="disabled" v-model="examName" />
          </span>
        </div>
        <div class="set-ruleEdit-container">
          <div class="set-container" :class="{ 'container-labelShow': labelShow }" style="overflow: hidden;">
            <span class="set-ruleEdit-label">题目分类：</span>
            <a-checkbox-group @change="onChange" v-model="paperType" style="width: calc(100% - 157px);">
              <a-row>
                <a-col
                  style="line-height: 30px; float: left; min-width: 12%;"
                  v-for="item in paperTypeList"
                  :key="item.Id"
                >
                  <a-checkbox :disabled="disabled" :value="item">{{ item.Name }}</a-checkbox>
                </a-col>
                <a-col :span="3" style="line-height: 30px;color:#1E8DFD;cursor: pointer;" @click="visible = true">
                  <router-link to="/teacher/questionManagement">
                    <span> <a-icon type="plus-circle" />添加新分类 </span>
                  </router-link>
                </a-col>
              </a-row>
            </a-checkbox-group>
            <span class="set-ruleEdit-label label-btn" @click="getCheckbox" v-if="paperTypeList.length > 0">
              <i class="iconfont icon-zhankai1" :class="{ 'btn-labelShow': labelShow }"></i>
              {{ btnTitle }}
            </span>
          </div>
        </div>
        <div class="set-ruleEdit-container">
          <span class="set-ruleEdit-label">多选题计分方式：</span>
          <a-radio-group name="radioGroup" :disabled="disabled" v-model="way">
            <a-radio :value="1">多选、少选、错选不得分</a-radio>
            <a-radio :value="2">少选时，按照选项个数计分</a-radio>
            <a-radio :value="3">少选时，得一半分数</a-radio>
          </a-radio-group>
        </div>
        <div class="set-ruleEdit-container">
          <span class="set-ruleEdit-label">备注：</span>
          <span class="oe-manage-input">
            <a-input placeholder="请输入备注信息" :disabled="disabled" v-model="remarks" />
          </span>
        </div>
      </div>
    </div>
    <question-type
      title="考试规则"
      saveTitle="保存规则"
      :curType="false"
      :disabled="disabled"
      :isShowBtn="disabled"
      :courseNameList="paperType"
      :listeningTrigger="listeningTrigger"
      @onChangeCondition="getQuesTypeCount"
      @onSave="onSave"
      @onCancel="onCancel"
    ></question-type>
  </div>
</template>

<script>
import topTip from '@/components/top-tip/index.vue';
import questionType from '@/components/question-type/index.vue';
export default {
  components: {
    topTip,
    questionType
  },
  data() {
    return {
      disabled: true,
      visible: false, // 新增考试分类modal可见
      examTitle: '新增考试分类',
      loading: false,
      title: [{ text: '规则管理' }, { text: '查看', color: '#1890ff' }],
      mode: 0,
      randomExamRuleId: '',
      examName: '', // 规则标题
      remarks: '', // 备注
      paperTypeList: [], // 题目列表
      paperType: [], // 选中的题目列表
      rulesDetails: [], // 详情
      listeningTrigger: false, // 深度监听触发装置
      way: 1,
      labelShow: false,
      btnTitle: '展开',
      paperNumber: 1,
      knowledge: ''
    };
  },
  created() {
    this.getType();
  },
  watch: {
    paperType(newValue, oldValue) {
      this.getFor(newValue);
    }
  },
  methods: {
    // 编辑|| 新增
    getType() {
      let examid = this.$route.params.examid;
      let typeTitle = examid !== undefined ? '编辑' : '新增';
      this.title = [{ text: '规则管理' }, { text: typeTitle, color: '#1890ff' }];
      this.randomExamRuleId = examid !== undefined ? examid : '';
      this.getRule();
    },
    // 获取题目列表||编辑规则 id 存在编辑
    getRule() {
      const urlApi = '/api/design/examrules/getQuestionClassList';
      const prames = { randomExamRuleId: this.randomExamRuleId };
      let that = this;
      this.$http.post(urlApi, prames).then((res) => {
        let courseList = res.courseList.map((v) => {
          return {
            ...v,
            ruleKnowledgeList: []
          };
        });
        if (res.code == 1) {
          that.paperTypeList = courseList;
          that.paperType = courseList.filter((item) => item.IsCheck === true);
          if (res.ruleInfo !== null) {
            that.examName = res.ruleInfo.RuleTitle;
            that.remarks = res.ruleInfo.Remarks;
            that.way = res.ruleInfo.MultiScoreWays;
          }
        } else {
          that.$message.error(res.msg);
        }
      });
    },
    // 获取知识点列表
    getRuleKnowledgeDat(id, index) {
      const urlApi = '/api/design/examrules/getRuleKnowledgeData';
      const prames = { courseId: id, randomExamRuleId: this.randomExamRuleId };
      let that = this;
      this.$http.post(urlApi, prames).then((res) => {
        that.paperType[index].ruleKnowledgeList = res.list.filter((item) => item.name.length > 0);
        let knowledgeArray = res.list.filter((item) => item.checked == true);
        let list = { knowledgeIds: [], defficultys: [] };
        list.knowledgeIds = knowledgeArray.map((v) => {
          return v.id;
        });
        list.defficultys = res.defficultyLevel ? res.defficultyLevel.split(',').map(Number) : [1, 2, 3, 4, 5];
        that.getQuesTypeCount(id, index, list);
        console.log(that.paperType, list, '知识点列表');
      });
    },
    // 循环获取详情
    getFor(array) {
      let that = this;
      for (var i = 0; i < array.length; i++) {
        that.getRuleKnowledgeDat(array[i].Id, i);
      }
    },
    // 获取规则详情
    getQuesTypeCount(courseId, index, list) {
      console.log(courseId, index, list, '规则详情');
      const urlApi = '/api/design/examrules/getQuesTypeCount';
      let defficultys = list.defficultys ? list.defficultys.join(',') : '1,2,3,4,5';
      let onLineKnowledgeIds = list.knowledgeIds ? list.knowledgeIds.join(',') : '';
      const prames = { randomExamRuleId: this.randomExamRuleId, courseId, defficultys, onLineKnowledgeIds };
      let that = this;
      this.$http.post(urlApi, prames).then((res) => {
        if (res.code == 1) {
          that.paperType[index].data = that.arrRegroup(res, courseId);
          console.log(that.arrRegroup(res, courseId), 111);
          that.paperType[index].knowledgeIds = list.knowledgeIds;
          that.paperType[index].knowledgeTitles = res.knowledgeTitles;
          that.paperType[index].defficultys = list.defficultys;
          that.listeningTrigger = !that.listeningTrigger;
          console.log(that.paperType, '规则详情');
        } else {
          that.$message.error(res.msg);
        }
      });
    },
    // 保存试卷||保存规则
    onSave(quesJson) {
      const urlApi = '/api/design/examrules/saveExamRule';
      const prames = {
        randomExamRuleId: this.randomExamRuleId,
        randomExamRuleTitle: this.examName,
        quesJson,
        multiScoreWays: this.way,
        remarks: this.remarks
      };
      let that = this;
      this.$http.post(urlApi, prames).then((res) => {
        if (res.code == 1) {
          that.$message.success(res.msg);
          this.$router.back();
        } else {
          that.$message.error(res.msg);
        }
      });
    },
    // 取消
    onCancel() {
      this.paperType = [];
    },
    getModeType() {
      if (this.mode == 0) {
        this.titleType = '随机出题';
      } else if (this.mode == 1) {
        this.titleType = '手动出题';
      }
    },
    getPopupContainer(trigger) {
      return trigger.parentElement;
    },
    getCheckbox() {
      if (this.labelShow) {
        this.labelShow = false;
        this.btnTitle = '展开';
      } else {
        this.labelShow = true;
        this.btnTitle = '收起';
      }
    },
    // 展示数据data
    arrRegroup(res, courseId) {
      let mergeArray = res.list;
      let mainArray = [
        {
          title: '单选题',
          subTitle: '(客观)',
          questionType: 1,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '多选题',
          subTitle: '(客观)',
          questionType: 2,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '判断题',
          subTitle: '(客观)',
          questionType: 3,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '填空题',
          subTitle: '(客观)',
          questionType: 4,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '填空题',
          subTitle: '(主观)',
          questionType: 5,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '问答题',
          subTitle: '(主观)',
          questionType: 6,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '匹配题',
          subTitle: '(客观)',
          questionType: 7,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '阅读理解',
          subTitle: '(客观)',
          questionType: 8,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '完形填空',
          subTitle: '(客观)',
          questionType: 9,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '视听题',
          subTitle: '(客观)',
          questionType: 11,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '阅读理解',
          subTitle: '(主观)',
          questionType: 12,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        },
        {
          title: '完形填空',
          subTitle: '(主观)',
          questionType: 13,
          total: 0,
          count: 0,
          randomQuesNumber: 0,
          randomQuesScore: 0,
          courseId: '',
          onLineKnowledgeIds: '',
          onLineKnowledgeTitles: '',
          defficultys: ''
        }
      ];
      for (var n = 0; n < mainArray.length; n++) {
        for (var i = 0; i < mergeArray.length; i++) {
          if (mergeArray[i].QuestionType == mainArray[n].questionType) {
            mainArray[n].randomQuesNumber = mergeArray[i].ChooseCount;
            mainArray[n].randomQuesScore = mergeArray[i].ChooseScore;
            mainArray[n].count = mergeArray[i].Count;
          }
        }
        mainArray[n].total = mainArray[n].randomQuesNumber * mainArray[n].randomQuesScore;
      }
      console.log(mainArray, '展示数据data');
      return mainArray;
    }
  }
};
</script>

<style></style>
